home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / hdf / unix / examples.lha / examples / ris8 / manyRIS.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-28  |  4.6 KB  |  215 lines

  1. /*****************************************************************
  2. *
  3. * manyRIS.c
  4. *
  5. * Program to generate and write many images of many different sizes
  6. * as RISs, then read them back and verify their correctness.
  7. *
  8. * User supplies number of images and file name.
  9. *
  10. * NOTE: Currently does not test imcomp compression.
  11. *
  12. ******************************************************************/
  13.  
  14. #include "df.h"
  15. #include <stdio.h>
  16. #ifdef __STDC__
  17. #include <stdlib.h>
  18. #else
  19. #include <malloc.h>
  20. #endif
  21.  
  22.  
  23. #define NUM_PAL  2
  24. #define PAL_SIZE 768
  25. #define DIM_MAX  100.0
  26. #define TRUE     1
  27. #define FALSE     0
  28.  
  29. static xsizes[] =    {100, 50, 20, 10,  99,  52,  23, 100, 17, 51 };
  30. static ysizes[] =    {100, 50, 20, 10, 100, 100, 100,  20, 23, 51 };
  31. static palflags[]  = { 0,   1,  0,  0,  0,   1,   1,    0,  0,  0 };
  32.  
  33. typedef struct {
  34.   int xdim, ydim;
  35.   char *img;
  36.   char *pal;
  37.   int comp;
  38. } record;
  39.  
  40. record *rec;
  41.  
  42. char *randArray();
  43. void putImages();
  44. void checkImages();
  45.  
  46. main(argc, argv)
  47.      int argc;
  48.      char *argv[];
  49. {
  50.   int i, numImages;
  51.   char *fname;
  52.  
  53.   if (argc < 3) {
  54.     puts("Usage :");
  55.     printf("\t%s <number of images> <file name>\n", argv[0]);
  56.     exit(1);
  57.   }
  58.  
  59.   numImages = atoi(argv[1]);
  60.   fname = argv[2];
  61.   printf("Test invoked with %d images and file %s.\n", numImages, fname);
  62.  
  63.   srand(getpid());
  64.   rec = (record *) malloc(numImages*sizeof(record));
  65.   if (rec == NULL) {
  66.     puts("--Calloc error");
  67.     exit(1);
  68.   }
  69.  
  70.   printf("\n===== Now putting images ==============\n\n");
  71.   putImages(fname, numImages);
  72.   printf("\n\n===== Now checking images ==============\n\n");
  73.   checkImages(fname, numImages);
  74. }
  75.  
  76. void
  77. putImages(fname, numImages)
  78.      char *fname;
  79.      int numImages;
  80. {
  81.   int i, ret;
  82.   int firstImage = TRUE;
  83.  
  84.   for (i=0;i<numImages;i++) {
  85.     printf("Putting image %d.\n", i);
  86.  
  87.     rec[i].xdim = xsizes[i%10];
  88.     rec[i].ydim = ysizes[i%10];
  89.     rec[i].img = randArray(rec[i].xdim * rec[i].ydim);
  90.     rec[i].comp = rndCompress(rec[i].xdim * rec[i].ydim);
  91.  
  92.     if (rec[i].comp == DFTAG_IMC)  /* REMOVE THIS WHEN IMCOMP IS FIXED */
  93.       rec[i].comp = DFTAG_RLE;
  94.  
  95.     printf("Image %d stats : %d * %d, %d compress scheme.\n",
  96.        i, rec[i].xdim, rec[i].ydim, rec[i].comp);
  97.  
  98.     if (palflags[i%10] == 1)  {
  99.       rec[i].pal = randArray(256);
  100.       printf("Palette included\n");
  101.     }else {
  102.       rec[i].pal = NULL;
  103.       printf("Palette not included\n");
  104.     }
  105.     ret = DFR8setpalette(rec[i].pal);
  106.     printf("DFR8setpalette return %d.\n", ret);
  107.  
  108.     if (firstImage) {
  109.       ret = DFR8putimage(fname, rec[i].img, rec[i].xdim,
  110.              rec[i].ydim, rec[i].comp);
  111.       printf("DFR8putimage return %d.\n", ret);
  112.       firstImage = FALSE;
  113.     }
  114.     else {
  115.       ret = DFR8addimage(fname, rec[i].img, rec[i].xdim,
  116.              rec[i].ydim, rec[i].comp);
  117.       printf("DFR8addimage return %d.\n", ret);
  118.     }
  119.     if (ret)
  120.       printf("DFerror %d.\n", DFerror);
  121.  
  122.     printf("\n");
  123.   }
  124. }
  125.  
  126. void
  127. checkImages(fname, numImages)
  128.      char *fname;
  129.      int numImages;
  130. {
  131.   int i, xdim, ydim, ispal, ret;
  132.   char space[10000], pal[768];
  133.   int all_ok = TRUE;
  134.  
  135.   for (i=0;i<numImages;i++) {
  136.     printf("checkImages image %d.\n", i);
  137.  
  138.     ret = DFR8getdims(fname, &xdim, &ydim, &ispal);
  139.     printf("DFR8getdims return %d.\n", ret);
  140.     if (ret) {
  141.       printf("DFerror %d.\n", DFerror);
  142. /*    exit(1);  */
  143.     }
  144.     printf("DFR8getdims %s, %d * %d, %d palette.\n", fname, xdim, ydim, ispal);
  145.  
  146.     if (rec[i].xdim != xdim){
  147.       puts("xdim is wrong!!");
  148.       all_ok = FALSE;
  149.     }
  150.     if (rec[i].ydim != ydim) {
  151.       puts("ydim is wrong!!");
  152.       all_ok = FALSE;
  153.     }
  154.     if ((ispal && (rec[i].pal == NULL)) ||
  155.     (!ispal && (rec[i].pal != NULL))) {
  156.       puts("ispal is wrong!!");
  157.       all_ok = FALSE;
  158.     }
  159.  
  160.     ret = DFR8getimage(fname, space, xdim, ydim, pal);
  161.     printf("DFR8getimage return %d.\n", ret);
  162.     if (ret) {
  163.       printf("DFerror %d.\n", DFerror);
  164. /*    exit(1); */
  165.     }
  166.  
  167.     if (strncmp(space, rec[i].img, xdim * ydim)) {
  168.       puts("Image is wrong!!");
  169.       all_ok = FALSE;
  170.     }
  171.     if (ispal && strncmp(pal, rec[i].pal, PAL_SIZE)) {
  172.       puts("Palette is wrong!!");
  173.       all_ok = FALSE;
  174.     }
  175.     printf("\n");
  176.   }
  177.   if (all_ok == TRUE) 
  178.     printf("HDF HAS WRITTEN AND READ ALL %d IMAGES CORRECTLY.\n\n", numImages);
  179. }
  180.  
  181. char *
  182. randArray(size)
  183.      int size;
  184. {
  185.   char *space;
  186.   int i;
  187.  
  188.   space = (char *) malloc(size);
  189.   if (space == NULL) {
  190.     puts("--malloc error");
  191.     exit(1);
  192.   }
  193.  
  194.   for (i=0;i<size;i++) 
  195.     space[i] = (i*size)%256;
  196.  
  197.   return space;
  198. }
  199.  
  200. int
  201. rndCompress(size)
  202. {
  203.   switch(size%3) {
  204.   case 0:
  205.     return 0;
  206.     break;            /* for uniformity */
  207.   case 1:
  208.     return DFTAG_RLE;
  209.     break;
  210.   case 2:
  211.     return DFTAG_IMC;
  212.     break;
  213.   }
  214. }
  215.